//package com.pro.jta.base.datasource;
//
//import java.sql.SQLException;
//import java.util.Properties;
//
//import javax.sql.DataSource;
//
//import org.apache.ibatis.plugin.Interceptor;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.mybatis.spring.SqlSessionFactoryBean;
//import org.mybatis.spring.SqlSessionTemplate;
//import org.mybatis.spring.mapper.MapperScannerConfigurer;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.transaction.annotation.EnableTransactionManagement;
//
//import com.atomikos.jdbc.AtomikosDataSourceBean;
//import com.github.pagehelper.PageHelper;
//import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
//
////@Configuration
////@EnableTransactionManagement
//public class DataSourceConfig {
//	// 配置数据源
//	@Primary
//	@Bean(name = "dataSource1")
//	public DataSource dataSource1(Databases1Config databases1Config)
//			throws SQLException {
//		MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
//		mysqlXaDataSource.setUrl(databases1Config.getUrl());
//		mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
//		mysqlXaDataSource.setPassword(databases1Config.getPassword());
//		mysqlXaDataSource.setUser(databases1Config.getUsername());
//		mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
//
//		AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
//		xaDataSource.setXaDataSource(mysqlXaDataSource);
//		xaDataSource.setUniqueResourceName("dataSource1");
//
//		xaDataSource.setMinPoolSize(databases1Config.getMinPoolSize());
//		xaDataSource.setMaxPoolSize(databases1Config.getMaxPoolSize());
//		xaDataSource.setMaxLifetime(databases1Config.getMaxLifetime());
//		xaDataSource.setBorrowConnectionTimeout(databases1Config
//				.getBorrowConnectionTimeout());
//		xaDataSource.setLoginTimeout(databases1Config.getLoginTimeout());
//		xaDataSource.setMaintenanceInterval(databases1Config
//				.getMaintenanceInterval());
//		xaDataSource.setMaxIdleTime(databases1Config.getMaxIdleTime());
//		xaDataSource.setTestQuery(databases1Config.getTestQuery());
//		return xaDataSource;
//	}
//
//	@Bean(name = "dataSource2")
//	public DataSource dataSource2(Databases2Config databases2Config)
//			throws SQLException {
//		MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
//		mysqlXaDataSource.setUrl(databases2Config.getUrl());
//		mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
//		mysqlXaDataSource.setPassword(databases2Config.getPassword());
//		mysqlXaDataSource.setUser(databases2Config.getUsername());
//		mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
//
//		AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
//		xaDataSource.setXaDataSource(mysqlXaDataSource);
//		xaDataSource.setUniqueResourceName("dataSource2");
//
//		xaDataSource.setMinPoolSize(databases2Config.getMinPoolSize());
//		xaDataSource.setMaxPoolSize(databases2Config.getMaxPoolSize());
//		xaDataSource.setMaxLifetime(databases2Config.getMaxLifetime());
//		xaDataSource.setBorrowConnectionTimeout(databases2Config
//				.getBorrowConnectionTimeout());
//		xaDataSource.setLoginTimeout(databases2Config.getLoginTimeout());
//		xaDataSource.setMaintenanceInterval(databases2Config
//				.getMaintenanceInterval());
//		xaDataSource.setMaxIdleTime(databases2Config.getMaxIdleTime());
//		xaDataSource.setTestQuery(databases2Config.getTestQuery());
//		return xaDataSource;
//	}
//
//	@Primary
//	@Bean(name = "sqlSessionFactory1")
//	public SqlSessionFactory sqlSessionFactory1(
//			@Qualifier("dataSource1") DataSource dataSource) throws Exception {
//		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//		bean.setDataSource(dataSource);
//
//		// 分页插件
//		PageHelper pageHelper = new PageHelper();
//		Properties properties = new Properties();
//		properties.setProperty("dialect", "mysql");
//		properties.setProperty("reasonable", "false");
//		properties.setProperty("pageSizeZero", "true");
//		pageHelper.setProperties(properties);
//		bean.setPlugins(new Interceptor[] { pageHelper });
//
//		bean.setMapperLocations(new PathMatchingResourcePatternResolver()
//				.getResources("classpath:/mybatis/databases1/*.xml"));
//		return bean.getObject();
//	}
//
//	@Bean(name = "sqlSessionFactory2")
//	public SqlSessionFactory sqlSessionFactory2(
//			@Qualifier("dataSource2") DataSource dataSource) throws Exception {
//		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//		bean.setDataSource(dataSource);
//
//		// 分页插件
//		PageHelper pageHelper = new PageHelper();
//		Properties properties = new Properties();
//		properties.setProperty("dialect", "mysql");
//		properties.setProperty("reasonable", "false");
//		properties.setProperty("pageSizeZero", "true");
//		pageHelper.setProperties(properties);
//		bean.setPlugins(new Interceptor[] { pageHelper });
//
//		bean.setMapperLocations(new PathMatchingResourcePatternResolver()
//				.getResources("classpath:/mybatis/databases2/*.xml"));
//		return bean.getObject();
//	}
//
//	@Primary
//	@Bean(name = "sqlSessionTemplate1")
//	public SqlSessionTemplate sqlSessionTemplate1(
//			@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory)
//			throws Exception {
//		return new SqlSessionTemplate(sqlSessionFactory);
//	}
//
//	@Bean(name = "sqlSessionTemplate2")
//	public SqlSessionTemplate sqlSessionTemplate2(
//			@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory)
//			throws Exception {
//		return new SqlSessionTemplate(sqlSessionFactory);
//	}
//
//	/**
//	 * - 设置SqlSessionFactory - 设置dao所在的package路径;
//	 */
//	@Bean
//	public MapperScannerConfigurer mapperScannerConfigurer1() {
//		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
//		mapperScannerConfigurer
//				.setSqlSessionFactoryBeanName("sqlSessionFactory1");
//		mapperScannerConfigurer.setBasePackage("com.pro.jta.mapper.databases1");
//		return mapperScannerConfigurer;
//	}
//
//	@Bean
//	public MapperScannerConfigurer mapperScannerConfigurer2() {
//		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
//		mapperScannerConfigurer
//				.setSqlSessionFactoryBeanName("sqlSessionFactory2");
//		mapperScannerConfigurer.setBasePackage("com.pro.jta.mapper.databases2");
//		return mapperScannerConfigurer;
//	}
//}